<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '办理'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="办理内容" prop="handleContent">
      <el-input type="textarea" v-model="dataForm.handleContent" placeholder=""></el-input>
    </el-form-item>
      <el-form-item label="上传文件" prop="handleFileId">
        <el-upload
          class="upload-demo"
          ref="upload"
          :action="uploadUrl"
          name="file"
          :limit="1"
          accept=".png,.jpg,.pdf,.doc,.docx,.xls,.xlsx"
          :before-remove="beforeRemove"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-upload="beforeUpload"
          :on-success="handleSuccess"
          :on-exceed="handleExceed"
          :file-list="fileList"
          :auto-upload="true">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip">只能上传后缀名为 .png/.jpg/.pdf/.doc/.docx/.xls/.xlsx的文件，且不超过100MB</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',

          createBy: '',

          updateTime: '',

          updateBy: '',

          isDel: '',

          unitId: '',

          unitName: '',

          title: '',

          content: '',

          fileId: '',

          fileName: '',

          receiveUnitId: '',

          receiveUnitName: '',

          status: '',

          beginTime: '',

          endTime: '',

          handleBy: '',

          handleName: '',

          handleTime: '',

          handleContent: '',

          handleFileId: '',

          handleFileName: '',

          isCs: '',

          type: ''

        },
        dataRule: {
          handleContent: [
            { required: true, message: '办理内容不能为空', trigger: 'blur' }
          ]
        },
        uploadUrl:'',
      }
    },
    methods: {
      handleDownload() {
        window.open(this.$http.adornUrl('/api-file/file/download/file?handleFileId=' + this.dataForm.fileId))
      },
      init (id) {
        this.uploadUrl = this.$http.adornUrl('/api-file/file/upload');
        this.dataForm.id = id || 0;
        this.fileList=[];
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          /*if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sms/task/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createTime = data.data.createTime
                this.dataForm.createBy = data.data.createBy
                this.dataForm.updateTime = data.data.updateTime
                this.dataForm.updateBy = data.data.updateBy
                this.dataForm.isDel = data.data.isDel
                this.dataForm.unitId = data.data.unitId
                this.dataForm.unitName = data.data.unitName
                this.dataForm.title = data.data.title
                this.dataForm.content = data.data.content
                this.dataForm.fileId = data.data.fileId
                this.dataForm.fileName = data.data.fileName
                this.dataForm.receiveUnitId = data.data.receiveUnitId
                this.dataForm.receiveUnitName = data.data.receiveUnitName
                this.dataForm.status = data.data.status
                this.dataForm.beginTime = data.data.beginTime
                this.dataForm.endTime = data.data.endTime
                this.dataForm.handleBy = data.data.handleBy
                this.dataForm.handleName = data.data.handleName
                this.dataForm.handleTime = data.data.handleTime
                this.dataForm.handleContent = data.data.handleContent
                this.dataForm.handleFileId = data.data.handleFileId
                this.dataForm.handleFileName = data.data.handleFileName
                this.dataForm.isCs = data.data.isCs
                this.dataForm.type = data.data.type
              }
            })
          }*/
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/api-operation/task/${!this.dataForm.id ? 'save' : 'banli'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,

                'handleContent': this.dataForm.handleContent,

                'handleFileId': this.dataForm.handleFileId,

                'handleFileName': this.dataForm.handleFileName,

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      beforeRemove (file, fileList) {
        return new Promise((response,reject)=>{
          this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              console.log(file);
              return response();
            })
            .catch(() => {
              return reject();
            });
        });
      },
      handleRemove (file, fileList) {
        this.deleteFile(file);
      },
      handlePreview (file) {
        window.open(this.$http.adornUrl('/api-file/file/view/file?fileId='+this.dataForm.handleFileId), '_blank').location;
        /*if(this.dataForm.postCertificate){
          window.open('/file/view/file?fileId='+this.dataForm.postCertificate, '_blank').location
        }else{
          this.$alert('请选择图片并上传到服务器！','重要提示', {
            confirmButtonText: '确定'
          })
        }*/
      },
      beforeUpload (file) {
        this.dataForm.handleFileName=file.name;
        const isLt100M = file.size / 1024 / 1024 < 100;
        if (!isLt100M) {
          this.$message.error('上传文件大小不能超过 100MB!')
        }
        return isLt100M
      },
      handleSuccess (response, file, fileList) {
        if (response && response.code === 0) {
          this.dataForm.handleFileId = response.data
        }

      },
      // 文件个数超出
      handleExceed() {
        this.$message.error(`只允许上传单个文件`);
      },
      // 删除文件
      deleteFile (file) {
        let that =this;
        this.$http({
          url: this.$http.adornUrl('/api-file/file/deleteByFileId'),
          method: 'get',
          params: this.$http.adornParams({
            'fileId': this.dataForm.handleFileId,
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            that.dataForm.handleFileId='';
            that.dataForm.handleFileName='';
            this.$alert('文件已删除！','重要提示', {
              confirmButtonText: '确定'
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 查看文件
      /* lookFile (id) {
         this.$http({
           url: ('/file/view/file?fileId' + id),
           method: 'get',
           params: this.$http.adornParams()
         }).then(({data}) => {
           if (data && data.code === 0) {
             this.fileList = data.data
           }
         })
       },*/
      dateFormCancel() {
        if (this.dataForm.fileId) {
          this.visible = false;
        }else {
          this.visible = false;
        }
      }
    }
  }
</script>
